import RenderCell from 'packages/BaseTableCell/renderCell.js';
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { formatDate } from 'pub-bbx-utils';
import { isOpenData } from '@src/util/platform'
import i18n from '@src/locales'
import { SessionSourceMap } from '@model/enum/ImEnum.js'
import { getRootWindow } from '@src/util/dom';

function isOpenMailIM() {
  return true
}

function getSessionSource(){
  const list = [
    {
      text: SessionSourceMap['im'],
      value: 'im'
    },
    {
      text: SessionSourceMap['weChat'],
      value: 'weChat'
    },
  ]

  if(isOpenMailIM()) {
    list.push( {text: SessionSourceMap['emailService'],value: 'emailService'} )
  }
  
  return list
}

// 高级搜索
export const getAdvancedFields = (vm, AllPutChannel = [], AllServiceNumber = []) => {
  let that = vm;
  return [
    {
      fieldName: 'customer', // customerId
      displayName: i18n.t('common.fields.customerName.displayName'),
      formType: 'text',
    },
    {
      fieldName: 'linkmanId',
      displayName: i18n.t('common.fields.contact.displayName'),
      formType: 'select',
    },
    {
      fieldName: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource: getSessionSource()
      }
    },
    {
      fieldName: 'imStartTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
      formType: 'datetime',
      setting: {
        toString: true,
      },
    },
  ].map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: null,
      show: true,
      isSearch: 1,
      isNull: 1,
      isSystem: 1,
      orderId: 1
    }
  });
}


// 列表 && 选择列
export const getColumnFields = (vm) => {
  const that = vm;

  return [
    {
      fieldName: 'conversationNumber',
      displayName: i18n.t('common.fields.imConversationNumber.displayName'),
      sortName: 'conversationNumber',
      dataType: 'string',
      renderCell: (h, col, row) => {
        row.conversationNumber = row.conversationNo
        return RenderCell.renderIm.call(that, h, row, that.allowImView);
      }
    },
    {
      fieldName: 'customerName',
      displayName: i18n.t('common.fields.customerName.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerName}</div>
      }
    },
    {
      fieldName: 'linkmanName',
      displayName: i18n.t('common.fields.contact.displayName'),
      renderCell: (h, col, row) => {
        if (row.source == 'weChat' && row.linkmanName) {
          return <div>{row.linkmanName}<span class="wx-im iconfont icon-weixin5"></span></div>
        }
        return <div>{row.linkmanName}</div>
      }
    },
    {
      fieldName: 'phoneNumber',
      displayName: i18n.t('common.fields.phone.displayName'),
      renderCell: (h, col, row) => {
        return <div>{row.phoneNumber}</div>
      }
    },
    {
      fieldName: 'email',
      displayName: i18n.t('common.fields.email.displayName'),
      renderCell: (h, col, row) => {
        return <div>{row.email}</div>
      }
    },
    {
      fieldName: 'touristNo',
      displayName: i18n.t('common.fields.touristNo.displayName'),
      renderCell: (h, col, row) => {
        return <div>{row.touristNo}</div>;
      },
    },
    {
      fieldName: 'address',
      displayName: i18n.t('common.fields.address.displayName'),
      width: 210,
      renderCell: (h, col, row) => {
        return <div>{row.address}</div>
      }
    },
    {
      fieldName: 'nickname',
      displayName: i18n.t('common.fields.weChatNickname.displayName'),
      width: 210,
      renderCell: (h, col, row) => {
        return <div>{row.nickname}</div>
      }
    },
    {
      fieldName: 'gender',
      displayName: i18n.t('common.fields.sex.displayName'),
      renderCell: (h, col, row) => {
        if(row.source == 'weChat') return <div>{i18n.t('common.base.unknown')}</div>
        return <div>{row.linkmanSex}</div>
      }
    },
    {
      fieldName: 'startTime_1',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
    },

    // 注意等待时常升降序用 startTime字段
    {
      fieldName: 'waitTime',
      displayName: i18n.t('common.fields.imWaitTime.displayName'),
      sortable: 'custom',
      sortName: 'startTime',
      dataType: 'date',
    },
    {
      fieldName: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{ SessionSourceMap[row.source] || '' }</div>
      }
    },
    {
      fieldName: 'isTimeOut',
      displayName: i18n.t('common.fields.isTimeOut.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.isTimeOut ? i18n.t('common.base.yes') : i18n.t('common.base.no')}</div>;
      },
    },
    {
      fieldName: 'putChannelName',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.putChannelName ? row.putChannelName.join(',') : ''}</div>
      }
    },
  ].map(item => {
    // 初始化属性
    let field = {
      ...item,
      formType: 'text',
      setting: item.setting || {},
      isNull: 1,
      isSearch: 1,
      orderId: 1,
      show: true,
      tableName: 'event',
      minWidth: 150,
      visable: typeof item.visable == 'undefined' ? true : item.visable
    };
    return formatColumn(field);
  })
};

export const getExportSystemInfo = () => {
  return [
    {
      exportAlias: 'conversationNumber',
      displayName: i18n.t('common.fields.imConversationNumber.displayName'),
    },
    {
      exportAlias: 'customerName', // customerId
      displayName: i18n.t('common.fields.customerName.displayName'),
    },
    {
      exportAlias: 'linkmanName',
      displayName: i18n.t('common.fields.contact.displayName'),
    },
    {
      exportAlias: 'phoneNumber',
      displayName: i18n.t('common.fields.phone.displayName'),
    },
    {
      exportAlias: 'address',
      displayName: i18n.t('common.fields.address.displayName'),
    },
    {
      exportAlias: 'customerServiceNumber',
      displayName: i18n.t('common.fields.customerServiceNumber.displayName'),
    },
    {
      exportAlias: 'customerServiceName',
      displayName: i18n.t('common.fields.customerServiceName.displayName'),
    },
    {
      exportAlias: 'relationUserId',
      displayName: i18n.t('common.fields.relationUserId.displayName'),
    },
    {
      exportAlias: 'imStartTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
    },
    {
      exportAlias: 'imEndTime',
      displayName: i18n.t('common.fields.imEndTime.displayName'),
    },
    {
      exportAlias: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
    },
    {
      exportAlias: 'putChannel',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
    },
  ]
}